package com.example.work.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.work.entity.*;
import com.example.work.entity.vo.ItemScoreVo;
import com.example.work.entity.vo.PutItemScoreVo;
import com.example.work.mapper.TbCheckItemScoreMapper;
import com.example.work.service.IAreaItemService;
import com.example.work.service.IDbHospService;
import com.example.work.service.ITbCheckDeficiencyService;
import com.example.work.service.ITbCheckItemScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ITbCheckItemScoreServiceImpl extends ServiceImpl<TbCheckItemScoreMapper, TbCheckItemScore> implements ITbCheckItemScoreService {

    @Autowired
    private IAreaItemService itemService;

    @Autowired
    private IDbHospService dbHospService;

    @Autowired
    private ITbCheckDeficiencyService checkDeficiencyService;

    @Override
    public List<ItemScoreVo> getAreaItemsVo(String areaId, String batchId, String hospId) {

        // 获取所有的区域
        List<TbCheckAreaItem> itemList = itemService.getAreaItems(areaId);

        List<ItemScoreVo> itemScoreVoList = new ArrayList<>();
        for(TbCheckAreaItem item : itemList){
            TbCheckItemScore itemScore = TbCheckItemScore.getItemScoreInstance(item);
            itemScore.setBatchId(batchId);
            itemScore.setHospId(hospId);
            ItemScoreVo vo = new ItemScoreVo();
            vo.setItemScore(itemScore);
            vo.setItemName(item.getName());
            vo.setItemStandard(item.getStandard());
            vo.setScore(item.getScore());
            itemScoreVoList.add(vo);
        }

        return itemScoreVoList;
    }

    @Override
    public List<PutItemScoreVo> getPutAreaItemsVo(String areaId, String batchId, String hospId) {

        // 获取所有的区域
        List<TbCheckAreaItem> itemList = itemService.getAreaItems(areaId);

        List<PutItemScoreVo> itemScoreVoList = new ArrayList<>();
        for(TbCheckAreaItem item : itemList){
            TbCheckItemScore itemScore = TbCheckItemScore.getItemScoreInstance(item);
            itemScore.setBatchId(batchId);
            itemScore.setHospId(hospId);
            PutItemScoreVo vo = new PutItemScoreVo();
            vo.setItemScore(itemScore);
            vo.setItemName(item.getName());
            vo.setItemStandard(item.getStandard());
            vo.setScore(item.getScore());
            itemScoreVoList.add(vo);


            //获取问题留痕
            String categoryName = dbHospService.lambdaQuery()
                    .select(Dbhosp::getHospName)
                    .eq(Dbhosp::getHospId, hospId)
                    .oneOpt()
                    .map(Dbhosp::getHospName)
                    .orElse(null);
            vo.setGrid_name(categoryName);
            String remark = checkDeficiencyService.lambdaQuery()
                    .select(TbCheckDeficiency::getRemark)
                    .eq(TbCheckDeficiency::getObjectScoreId, item.getId())
                    .oneOpt()
                    .map(TbCheckDeficiency::getRemark)
                    .orElse(null);
            vo.setRemark(remark);

        }

        return itemScoreVoList;
    }

    @Override
    public List<TbCheckItemScore> getAreaItems(String areaId,String batchId) {
        return lambdaQuery()
                .eq(TbCheckItemScore::getBatchId,batchId)
                .eq(TbCheckItemScore::getAreaId, areaId)
                .eq(TbCheckItemScore::getDeleteFlag, false)
                .list();
    }


}
